local BaseData = require("data.base.BaseData")
local TeamGroupData = class("TeamGroupData", BaseData)

local schema = {}
schema["teamId"] = {"number", 0}
schema["leaderId"] = {"number", 0}
schema["targetId"] = {"number", 0}
schema["members"] = {"table", {}}
TeamGroupData.schema = schema

function TeamGroupData:ctor()
    TeamGroupData.super.ctor(self)

    self._signalId = G_NetWorkManager:beginCache()
    G_NetWorkManager:add(MessageIDConst.ID_S2C_CreateTeam, handler(self, self._s2cCreateTeam))
    G_NetWorkManager:add(MessageIDConst.ID_S2C_InviteTeam, handler(self, self._s2cInviteTeam))
    G_NetWorkManager:add(MessageIDConst.ID_S2C_DelTeam, handler(self, self._s2cDelTeam))
    G_NetWorkManager:add(MessageIDConst.ID_S2C_QuitTeam, handler(self, self._s2cQuitTeam))
    G_NetWorkManager:add(MessageIDConst.ID_S2C_NotifyTeam, handler(self, self._s2cNotifyTeam))
    G_NetWorkManager:add(MessageIDConst.ID_S2C_OptInviteTeam, handler(self, self._s2cOptInviteTeam))
    G_NetWorkManager:add(MessageIDConst.ID_S2C_TeamGroupEnterScene, handler(self, self._s2cTeamGroupEnterScene))
    G_NetWorkManager:endCache()
end

function TeamGroupData:clear()
    G_NetWorkManager:removeCache(self._signalId)
end

function TeamGroupData:clearTeam()
    local targetId = self:getTargetId()
    self:setTeamId(0)
    self:setLeaderId(0)
    self:setTargetId(0)
    self:setMembers({})
    G_SignalManager:dispatch(SignalConst.EVENT_DEL_TEAM_GROUP, targetId)
end

function TeamGroupData:c2sCreateTeam(targetId)
    local id = G_UserData:getBase():getId()
    G_NetWorkManager:send(MessageIDConst.ID_C2S_CreateTeam, {userId = id, targetId = targetId})
end

function TeamGroupData:c2sInviteTeam(userId)
    local id = G_UserData:getBase():getId()
    local targetId = self:getTargetId()
    G_NetWorkManager:send(MessageIDConst.ID_C2S_InviteTeam, {myId = id, userId = userId, targetId = targetId})
end

function TeamGroupData:c2sDelTeam()
    local teamId = self:getTeamId()
    if teamId == 0 then
        return
    end
    G_NetWorkManager:send(MessageIDConst.ID_C2S_DelTeam, {teamId = teamId})
end

function TeamGroupData:c2sQuitTeam()
    local teamId = self:getTeamId()
    if teamId == 0 then
        return
    end
    G_NetWorkManager:send(MessageIDConst.ID_C2S_QuitTeam, {teamId = teamId})
end

function TeamGroupData:c2sOptInviteTeam(teamId, opt)
    G_NetWorkManager:send(
        MessageIDConst.ID_C2S_OptInviteTeam,
        {
            teamId = teamId,
            opt = opt
        }
    )
end

function TeamGroupData:c2sTeamGroupEnterScene()
    local myId = G_UserData:getBase():getId()
    printDebug("TeamGroupData:c2sTeamGroupEnterScene 1")
    if myId == self:getLeaderId() then
        printDebug("TeamGroupData:c2sTeamGroupEnterScene 2")
        local teamId = self:getTeamId()
        local targetId = self:getTargetId()
        local cfg = require("config.monsterhole").get(targetId)
        G_NetWorkManager:send(MessageIDConst.ID_C2S_TeamGroupEnterScene, {teamId = teamId, sceneId = cfg.scene})
    end
end

function TeamGroupData:_s2cCreateTeam(id, message)
    if message.ret ~= 1 then
        return
    end
end

function TeamGroupData:_s2cDelTeam(id, message)
    local targetId = self:getTargetId()
    self:clearTeam()
end

function TeamGroupData:_s2cInviteTeam(id, message)
    if message.ret ~= 1 then
        return
    end
    local user = rawget(message, "user")
    local teamId = rawget(message, "teamId")
    local targetId = rawget(message, "targetId")
    local userData = {}
    userData.userName = string.gsub(user.userName, "\0", "")
    userData.userId = user.userId
    printDebug("TeamGroupData:_s2cInviteTeam ")
    G_SignalManager:dispatch(SignalConst.EVENT_INVITE_TEAM_GROUP, userData, teamId, targetId)
end

function TeamGroupData:_s2cNotifyTeam(id, message)
    local team = rawget(message, "team")
    local members = rawget(message, "members") or {}
    local teamId = rawget(team, "teamId")
    local leaderId = rawget(team, "leaderId")
    local targetId = rawget(team, "targetId")
    self:setTeamId(team.teamId)
    self:setLeaderId(team.leaderId)
    self:setTargetId(team.targetId)
    self:setMembers(members)
    G_SignalManager:dispatch(SignalConst.EVENT_TEAM_GROUP_NOTIFY)
end

function TeamGroupData:_s2cOptInviteTeam(id, message)
end

function TeamGroupData:_s2cQuitTeam(id, message)
    local targetId = self:getTargetId()
    self:clearTeam()
end

function TeamGroupData:_s2cTeamGroupEnterScene(id, message)
    -- int32 teamId =1;
    -- int32 sceneId =2;
    local teamId = rawget(message, "teamId")
    local sceneId = rawget(message, "sceneId")
    G_SignalManager:dispatch(SignalConst.EVENT_TEAM_GROUP_ENTER_SCENE, sceneId)
end

return TeamGroupData
